<!DOCTYPE html>
<html>
<head lang="en">
    <title>积分商城主页</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width = device-width , initial-scale = 1 , minimum-scale = 1 , maximum-scale = 1 , user-scalable = no">
    <link rel="stylesheet" href="../../resource/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../resource/css/common.css">
    <link rel="stylesheet" media="screen" href="../../resource/css/style.css">
</head>
<body>
<div id="header" style="border: none;">
    <a class="to_position back_1">&#xe732;返回</a>
    <a class="to_position back_2">&#xe732;返回</a>
    <h1 style="margin-left: -15%;">积分商城</h1>
</div>
<div class="first_page">
    <div class="integral_mall"></div>
    <div class="no_more"></div>
</div>
<div class="hide_page"></div>

<!--模态框 start-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body"></div>
            <div class="modal-footer">
                <button type="button" class="sanFast-btn"
                        data-dismiss="modal">取消
                </button>
                <button type="button" class="sanFast-btn release">
                    是
                </button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade position_change" id="Modal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body"></div>
            <div class="modal-footer">
                <button type="button" class="sanFast-btn"
                        data-dismiss="modal">返回
                </button>
            </div>
        </div>
    </div>
</div>


<script src="../../resource/js/jquery.min.js"></script>
<script src="../../resource/js/bootstrap.min.js"></script>
<script src="../../resource/js/commonjs.js"></script>
<script src="../../resource/js/ReceiveAndConvert.js"></script>
<script src="http://api.map.baidu.com/api?v=2.0&ak=qVrWQw9Vs8t7iPfG7bSj0137" type="text/javascript"></script>
<script>
    var page = null,no = null;
    var allLoad = null;//判断是否加载完毕
    var isFirstTime = null;//判断是否为第一次加载
    var loadControl = null;//控制每次只加载一下
    var longitude=null,latitude=null;
    var data_size=null;
    $(function(){
        init();
        loadMoreCouponBind();
        $(".back_1").click(function(){
            location.href="../../index.html?url=1";
        })
    })
    /*页面初始化渲染*/
    function init(){
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                longitude = r.point.lng;
                latitude = r.point.lat;
                allLoad = false;
                isFirstTime = true;
                page=1;
                no=3;
                loadingCouponShops();
            }
            else {
                alert('failed'+this.getStatus());
            }
        },{enableHighAccuracy: true})
        $("body").append("<div class='load-more loadImg'><img src='../../resource/images/loading.jpg'/></div>");
    }
    function loadingCouponShops(){
        var url = "/Home/getShopList";
        var arg = {};
        arg.lon=longitude;
        arg.lat=latitude;
        arg.page=page;
        arg.no=no;
        arg.is_credit=1;
        ajaxJsonp(url, arg, getCouponShops);
    }
    function getCouponShops(res){
        if(res.data.count==0){
            $(".first_page").append('<div class="noCouponResult">该地区没有积分优惠券信息！</div>');
            $(".load-more").hide();
        }else{
            var data = res.data.list;
            data_size=res.data.count;
            for ( var j=0; j <data.length; j++) {
                var shop_id = data[j].shop_id;
                var coupon_name=data[j].coupon_name;
                var image = data[j].image;
                var credit=data[j].credit;
                var coupon_id = data[j].coupon_id;
                var original_price=data[j].original_price;
                var present_price=data[j].present_price;
                var up_count=data[j].up_count;
                var remark=data[j].remark;
                var start_time = data[j].start_time;  //开始时间
                var expired_time = data[j].expired_time;  //结束时间
                var start_time1=timeChange(start_time).substr(2, 7);
                var expired_time1=timeChange(expired_time).substr(2, 7);
                var business_start_time = calculateTime(data[j].business_start_time);
                var business_end_time = calculateTime(data[j].business_end_time);
                var shop_couponList='<div class="mallList" onclick="linkToDetail(this)">'+
                        '<img src="'+image+'" title="点击查看源网页">'+
                        '<div class="zan_num" data-id="'+shop_id+'" onclick="zan(this,otherLinkLogin)">'+
                        '<div></div>'+
                        '<span class="heart_num">'+up_count+'</span>'+
                        '</div>'+
                        '<div class="listWords">'+
                        '<p>'+
                        '<span class="coupon_name">'+coupon_name+'</span>'+
                        '<label class="food_price">'+
                        '<span>现价：'+present_price+'</span>'+
                        '<span><del>'+original_price+'</del></span>'+
                        '</label>'+
                        '</p>'+
                        '<p>'+
                        '<span class="user_jifen">积分'+credit+'</span>'+
                        '<span class="duihuan" onclick="convert('+credit+','+coupon_id+',otherLinkLogin)">兑换</span>'+
                        '</p>'+
                        '</div>'+
                        '<div class="workTime" style="display:none;">'+business_start_time+'-'+business_end_time+'</div>'+
                        '<div class="useTime" style="display:none;">'+start_time1+'-'+expired_time1+'</div>'+
                        '<div class="remark" style="display:none;">'+remark+'</div>'+
                        '<div class="coupon_id" style="display:none;">'+coupon_id+'</div>'+
                        '</div>'
                $(".first_page>.integral_mall").append(shop_couponList);
                getShopIsUserGiving(shop_id);
            }
            $(".load-more").hide();
            loadControl = true;
            $(".no_more").text("下拉刷新加载更多……");
            $(".no_more").show();
        }

    }

    /* 滚动加载 */
    function loadMoreCouponBind(){
        $(window).scroll(function() {
            var scrollHeight = $(window).scrollTop();
            var totalHeight = document.body.clientHeight;
            var viewHeight = $(window).height();
            if(no*page >= data_size) {
                allLoad = true;
                $(".no_more").text("没有更多了……");
                $(".no_more").show();
            }
            if((scrollHeight + viewHeight) >= totalHeight && !allLoad) {
                page++;
                if(loadControl) {
                    loadControl = false;
                    isFirstTime = false;
                    loadingCouponShops();
                }
            }
        })
    }

   /*跳转详情页面*/
    function linkToDetail(target){
        $(".first_page").hide();
        $(".back_1").hide();
        $(".back_2").show();
        $(".hide_page>.integral_mall").remove();
        var coupon_name=$(target).find(".coupon_name").text();
        var image=$(target).find("img").attr("src");
        var up_count=$(target).find(".heart_num").text();
        var present_price=$(target).find(".food_price span:nth-of-type(1)").text();
        var original_price=$(target).find(".food_price span:nth-of-type(2)").text();
        var credit=$(target).find(".user_jifen").text();
        var credit_1=credit.substr(2);
        var remark=$(target).find(".remark").text();
        var useTime=$(target).find(".useTime").text();
        var workTime=$(target).find(".workTime").text();
        var coupon_id=$(target).find(".coupon_id").text();
        var shop_id=$(target).find(".zan_num").attr("data-id");
        var coupon_shop='<div class="integral_mall">'+
                            '<div class="mallList oneMall_bottom">'+
                                '<img src="'+image+'" title="点击查看源网页">'+
                                '<div class="zan_num" onclick="zan(this,otherLinkLogin)" data-id="'+shop_id+'">'+
                                    '<div class="'+shop_id+'"></div>'+
                                    '<span class="heart_num">'+up_count+'</span>'+
                                '</div>'+
                                '<div class="listWords" id="listWords">'+
                                    '<p>'+
                                        '<span class="coupon_name">'+coupon_name+'</span>'+
                                        '<label class="food_price">'+
                                            '<span>'+present_price+'</span>'+
                                            '<span><del>'+original_price+'</del></span>'+
                                        '</label>'+
                                    '</p>'+
                                '</div>'+
                            '</div>'+
                            '<div class="oneMall_detail">'+
                                '<div class="meal_detail">'+
                                    '<h2>套餐详情</h2>'+
                                    '<pre>'+remark+'</pre>'+
                                '</div>'+
                                '<div class="meal_detail">'+
                                    '<h2>温馨提示</h2>'+
                                    '<ul>'+
                                        '<li>使用日期：'+useTime+'</li>'+
                                        '<li>使用时段：'+workTime+'</li>'+
                                        '<li>请提前一天预约</li>'+
                                    '</ul>'+
                                '</div>'+
                            '</div>'+
                            '<div class="oneMall_jifen">'+
                                '<span>'+credit+'</span>'+
                                '<span class="duihuan" onclick="convert('+credit_1+','+coupon_id+',otherLinkLogin)">兑换</span>'+
                                '<p class="jifen_use" onclick="useCoupon('+coupon_id+')">使用</p>'+
                            '</div>'+
                        '</div>'
        $(".hide_page").append(coupon_shop);
        $(".hide_page").show();
        if($(target).find(".zan_num div").hasClass("heart")){
            $('.'+shop_id).addClass("heart");
        }
        $(".back_2").click(function(){
            $('.back_2').css({'color':'rgb(0,170,144)'});
            $(".back_1").show();
            location.href="integral_index.html?url=1";
            $(".back_2").hide();
        })
    }
    function useCoupon(coupon_id){
        $('#myModal .modal-body').text("是否使用该优惠券？");
        $("#myModal .modal-footer button:nth-of-type(1)").text("取消");
        $("#myModal .modal-footer button:nth-of-type(2)").text("是");
        $('#myModal').modal();
        $("#myModal .modal-footer button:nth-of-type(2)").click(function(){
            location.href="../userInfo/userUseCoupon.html?code_id=" + coupon_id;
        })
    }
</script>

</body>
</html>
